#include "link_pub.h"

typedef struct {
    char name[50];
    int credit;
    int hours;
    int studentCount;
    char students[MAX_STUDENTS][50];
}Course;

Course courses2[MAX_COURSES];
int courseCount2 = 0;

typedef struct {
	char stu_id[15];
    char stu_name[15];
    char stu_CourseList[10][20];
    int count;
    int total;
} Student;

Student students2[MAX_STUDENTS];
int numStudents2 = 0;

extern void ter();

extern Course creatcourse_ter();

extern void addCourse_ter();

extern int find_ter(char *name);

extern void deleteCourse_ter(char *name);

extern void printcourse_ter(int x);

extern void showCourse_ter(char *name);

extern void updateCourse_ter(char *name);

extern void displayCourseInfo_ter();

extern void course_ter();

extern void student_ter();

extern void print_ter(int x);

extern Student creatstudent_ter();

extern void byname_ter(int flag);

extern void bynumber_ter(int flag);

extern void modify_search_ter(int x,int flag);

extern void addStudent_ter(); 

extern void displayStudents_ter();

extern void deleteStudent_ter();

extern void searchStudent_ter();

extern void modifyStudent_ter();

void ter() {
    int choice;
    do {
        printf("请选择要进入的系统\n");
        printf("1. 学生信息管理系统\n");
        printf("2. 课程管理系统\n");
        printf("3. 退出\n");
        printf("输入你的选择: ");
        scanf("%d", &choice);
		
        switch (choice) {
            case 1:
                student_ter();
                break;
            case 2:
                course_ter();
                break;
            case 3:
                printf("退出程序.\n");
                return ;
            default:
                printf("无效的选择，请重试。\n");
        }

        printf("\n");
    } while (1);
    
}

void student_ter(){
	int choice;
	do {
        printf("学生信息管理系统\n");
        printf("1. 添加学生信息\n");
        printf("2. 显示学生信息\n");
        printf("3. 搜索学生\n");
        printf("4. 删除学生\n");
        printf("5. 修改学生信息\n");
        printf("6. 退出\n");
        printf("输入你的选择: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                addStudent_ter();
                break;
            case 2:
                displayStudents_ter();
                break;
            case 3:
                searchStudent_ter();
                break;
            case 4:
                deleteStudent_ter();
                break;
            case 5:
                modifyStudent_ter();
                break;
            case 6:
                printf("退出程序.\n");
                return ;
            default:
                printf("无效的选择，请重试。\n");
        }

        printf("\n");
    } while (1);
} 

Student creatstudent_ter(){
	int c;
	Student newStudent;
    newStudent.count=0;
    newStudent.total=0;
    printf("请输入学生学号: ");
    scanf("%s", newStudent.stu_id);
    printf("请输入学生姓名: ");
    scanf("%s", newStudent.stu_name);
    
    printf("请输入学生所选课程(输入*结束): ");
    while(1){
    	scanf("%s",newStudent.stu_CourseList[newStudent.count]);
    	if(!strcmp(newStudent.stu_CourseList[newStudent.count],"*")){
    		newStudent.count--;
    		break;
		}
		printf("请输入该课程学分: ");
    	scanf("%d",&c);
    	newStudent.total+=c;
    	printf("请输入学生所选课程(输入*结束): ");
    	newStudent.count++;
	}
    return newStudent;
}

void print_ter(int x){
	int n=0;
	printf("\n-----------------------\n");
	printf("学号: %s\n", students2[x].stu_id);
    printf("姓名: %s\n", students2[x].stu_name);
    printf("已选课程列表:\n");
    while(students2[x].count>=n){
    	printf("%s\n",students2[x].stu_CourseList[n]);
    	n++;
	}
	printf("已选课程总计学分: %d\n", students2[x].total);
    printf("\n-----------------------\n");
}

void addStudent_ter() {
    if (numStudents2 >= MAX_STUDENTS) {
        printf("录入学生数量已达上限\n");
        return;
    } else {
    	students2[numStudents2] = creatstudent_ter();
    	numStudents2++;
	}
}

void byname_ter(int flag){
	char searchName[15];
	int found = 0;
	if(flag==1){
		printf("请输入要修改学生姓名: ");
	}
	else if(flag==2){
		printf("请输入要搜索学生姓名: ");
	} 
    scanf("%s", searchName);
    for (int i = 0; i < numStudents2; i++) {
        if (strcmp(students2[i].stu_name, searchName) == 0) {
            modify_search_ter(i,flag);
            found = 1;
            break;
        }
    }
    if (!found) {
        printf("未找到该学生\n");
    }
} 

void bynumber_ter(int flag) {
	char id[15];
    int found = 0,n=0;
    if(flag==1){
		printf("请输入要修改学生学号: ");
	}
	else if(flag==2){
		printf("请输入要搜索学生学号: ");
	} 
    scanf("%s", id);
    for(int i = 0; i < numStudents2; i++) {
        if (strcmp(students2[i].stu_id, id) == 0) {
            modify_search_ter(i,flag);
            found = 1;
            break;
        }
    }

    if (!found) {
        printf("未找到该学生\n");
    }
}

void displayStudents_ter() {
    printf("学生信息：\n");
    int n=0;
    for (int i = 0; i < numStudents2; i++) {
    	print_ter(i);
    }
}

void deleteStudent_ter() {
    char deleteName[15];
    int deleteIndex = -1;
    
    printf("输入要删除学生姓名: ");
    scanf("%s", deleteName);

    for (int i = 0; i < numStudents2; i++) {
        if (strcmp(students2[i].stu_name, deleteName) == 0) {
            deleteIndex = i;
            break;
        }
    }

    if (deleteIndex != -1) {
        for (int i = deleteIndex; i < numStudents2 - 1; i++) {
            students2[i] = students2[i + 1];
        }
        numStudents2--;
        printf("学生已被删除\n");
    }
    else {
        printf("学生未找到\n");
    }
}

void modify_search_ter(int x,int flag) {
	int c,n=0;
    if(flag==1) {
    	FILE *log_file = fopen("RZ2.txt", "a");//rizhidizhi
	    if (log_file == NULL) {
	        printf("日志文件打开失败\n");
	        return;
	    }
	    fprintf(log_file, "1\t%s\t学生信息被修改\n", students2[x].stu_id);
	    fclose(log_file);
    	students2[x]=creatstudent_ter();
		printf("\n修改完成!");
	} else if(flag==2) {
		print_ter(x);
	}
}

void searchStudent_ter() {
    int choice;
    
    printf("输入所需查询模式:\n");
    printf("1. 通过学生姓名 \n");
    printf("2. 通过学生学号\n");
    printf("3. 取消查询\n");
    printf("输入你的选择: ");
    scanf("%d", &choice);
    
    switch (choice) {
        case 1:
            byname_ter(2);
            break;
        case 2:
            bynumber_ter(2);
            break;
        case 3:
            printf("查询被取消.\n");
            break;
        default:
            printf("无效的选择。请重试。\n");
    }
}

void modifyStudent_ter() {
    int choice;
    
    printf("输入所需修改模式:\n");
    printf("1. 通过学生姓名 \n");
    printf("2. 通过学生学号\n");
    printf("3. 取消修改\n");
    printf("输入你的选择: ");
    scanf("%d", &choice);
    
    switch (choice) {
        case 1:
            byname_ter(1);
            break;
        case 2:
            bynumber_ter(1);
            break;
        case 3:
            printf("修改被取消.\n");
            break;
        default:
            printf("无效的选择。请重试。\n");
    }
}

//-------------------------------------------------------------------------

Course creatcourse_ter() {
	Course newCourse;

    printf("请输入课程名称: ");
    scanf("%s", newCourse.name);
    printf("请输入学分: ");
    scanf("%d", &newCourse.credit);
    printf("请输入课时: ");
    scanf("%d", &newCourse.hours);
    newCourse.studentCount = 0;
    while(1){
    	printf("请输入选择该课程学生姓名(输入*结束): ");
    	scanf("%s",newCourse.students[newCourse.studentCount]);
    	if(!strcmp(newCourse.students[newCourse.studentCount],"*")){
    		newCourse.studentCount--;
    		break;
		}
    	newCourse.studentCount++;
	}
    return newCourse;
} 

void addCourse_ter() {
    courses2[courseCount2++] = creatcourse_ter();
    printf("课程添加成功!\n");
}

int find_ter(char *name) {
    for (int i = 0; i < courseCount2; i++) {
        if (strcmp(name, courses2[i].name) == 0) {
            return i;
            break;
        }
    }
	return -1;
}

void deleteCourse_ter(char *name) {
    int i, courseIndex = find_ter(name);

    if (courseIndex == -1) {
        printf("找不到该课程!\n");
        return;
    }

    for (i = courseIndex; i < courseCount2 - 1; i++) {
        courses2[i] = courses2[i + 1];
    }

    courseCount2--;

    printf("课程删除成功!\n");
}

void printcourse_ter(int x) {
	printf("课程名称: %s\n",courses2[x].name);
    printf("学分: %d\n", courses2[x].credit);
    printf("课时: %d\n", courses2[x].hours);
    printf("选修学生列表:\n");
    for (int i = 0; i <= courses2[x].studentCount; i++) {
        printf("%s\n", courses2[x].students[i]);
    }
} 

void showCourse_ter(char *name) {
    int i, courseIndex = find_ter(name);

    if (courseIndex == -1) {
        printf("找不到该课程!\n");
        return;
    }
    printcourse_ter(courseIndex);
}

void updateCourse_ter(char *name) {
    int i, courseIndex = find_ter(name);

    if (courseIndex == -1) {
        printf("找不到该课程!\n");
        return;
    }
	FILE *log_file = fopen("RZ2.txt", "a");//rizhidizhi
    if (log_file == NULL) {
        printf("日志文件打开失败\n");
        return;
    }
    fprintf(log_file, "2\t%s\t课程信息被修改\n", courses2[courseIndex].name);
    fclose(log_file);
    courses2[courseIndex] = creatcourse_ter();

    printf("课程信息更新成功!\n");
}

void displayCourseInfo_ter() {
    if (courseCount2 == 0) {
        printf("没有可展示的课程信息!\n");
        return;
    }

    printf("------------所有课程信息--------------------\n");
    for (int i = 0; i < courseCount2; i++) {
        printcourse_ter(i);
    }
}

void course_ter(){
	int choice;
    char courseName[50];

    while (1) {
        printf("\n课程管理系统\n");
        printf("1. 新增课程\n");
        printf("2. 删除课程\n");
        printf("3. 查看课程详细信息\n");
        printf("4. 修改课程信息\n");
        printf("5. 显示所有课程信息\n");
        printf("6. 退出\n");
        printf("请选择操作: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                addCourse_ter();
                break;
            case 2:
                printf("请输入要删除的课程名称: ");
                scanf("%s", courseName);
                deleteCourse_ter(courseName);
                break;
            case 3:
                printf("请输入要查看的课程名称: ");
                scanf("%s", courseName);
                showCourse_ter(courseName);
                break;
            case 4:
                printf("请输入要修改的课程名称: ");
                scanf("%s", courseName);
                updateCourse_ter(courseName);
                break;
            case 5:
                displayCourseInfo_ter();
                break;
            case 6:
                return ;
            default:
                printf("无效的选择!\n");
                break;
        }
    }
}
